//
//  BaoMingView.m
//  XRSquare
//
//  Created by 穆相臣 on 2018/9/4.
//  Copyright © 2018年 UaenaMo. All rights reserved.
//

#import "BaoMingView.h"

@interface BaoMingView ()
{
    UIView * contentView;
    UILabel * titleLabel;
    UIButton * timeStart;
    UIButton * timeEnd;
    UIButton * conform;
    UIView * weekDay;
    UIButton * remove;
    
    NSString * beginDay;
    NSString * endDay;

    NSMutableArray * selectedArr;
}
@end

@implementation BaoMingView

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)])
    {
        selectedArr = [NSMutableArray array];
        
        UIView * black = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
        black.alpha = 0.3;
        black.backgroundColor = [UIColor blackColor];
        [self addSubview:black];
        
        contentView = [[UIView alloc]initWithFrame:CGRectMake(30, ScreenHeight/2 - 170, ScreenWidth - 60, 170 + 80 + 50)];
        contentView.layer.cornerRadius = 10;
        contentView.clipsToBounds = YES;
        contentView.backgroundColor = [UIColor whiteColor];
        [self addSubview:contentView];
        
        titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth - 60, 50)];
        titleLabel.backgroundColor = themeColor;
        titleLabel.font = [UIFont systemFontOfSize:15];
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.textColor = [UIColor whiteColor];
        titleLabel.text = @"报名";
        [contentView addSubview:titleLabel];
        
        timeStart = [[UIButton alloc]initWithFrame:CGRectMake(0, 50, ScreenWidth - 60, 50)];
        timeStart.titleLabel.font = [UIFont systemFontOfSize:15];
        [timeStart setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [timeStart setTitle:@"开始时间" forState:UIControlStateNormal];
        [timeStart addTarget:self action:@selector(startTimeAction) forControlEvents:UIControlEventTouchUpInside];
        [contentView addSubview:timeStart];
        
        UILabel * content = [[UILabel alloc]initWithFrame:CGRectMake(0, 100, ScreenWidth - 60, 20)];
        content.font = [UIFont systemFontOfSize:15];
        content.textAlignment = NSTextAlignmentCenter;
        content.text = @"to";
        [contentView addSubview:content];
        
        timeEnd = [[UIButton alloc]initWithFrame:CGRectMake(0, 120, ScreenWidth - 60, 50)];
        timeEnd.titleLabel.font = [UIFont systemFontOfSize:15];
        [timeEnd setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [timeEnd setTitle:@"结束时间" forState:UIControlStateNormal];
        [timeEnd addTarget:self action:@selector(endTimeAction) forControlEvents:UIControlEventTouchUpInside];
        [contentView addSubview:timeEnd];
        
        weekDay = [[UIView alloc]initWithFrame:CGRectMake(0, 170, ScreenWidth - 60, 80)];
        [contentView addSubview:weekDay];
        
        
        conform = [[UIButton alloc]initWithFrame:CGRectMake(contentView.frame.size.width/2 - 30, 260, 60, 30)];
        conform.titleLabel.font = [UIFont systemFontOfSize:15];
        conform.layer.cornerRadius = 15;
        conform.clipsToBounds = YES;
        conform.backgroundColor = themeColor;
        [conform setTitle:@"确认" forState:UIControlStateNormal];
        [conform addTarget:self action:@selector(conformAction) forControlEvents:UIControlEventTouchUpInside];
        [contentView addSubview:conform];
        
        remove = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
        [remove setImage:[UIImage imageNamed:@"delete_fill"] forState:UIControlStateNormal];
        [remove addTarget:self action:@selector(removeAction) forControlEvents:UIControlEventTouchUpInside];
        remove.center = CGPointMake(ScreenWidth/2, ScreenHeight/2 + 150);
        [self addSubview:remove];
    }
    return self;
}

-(void)setWeek:(NSArray *)week
{
    _week = week;

    for (int i = 0; i < [week count]; i ++)
    {
        if (i < 4)
        {
            UIButton * button = [[UIButton alloc]initWithFrame:CGRectMake(10 + 60 * i, 10, 50, 20)];
            button.layer.cornerRadius = 10;
            button.titleLabel.font = [UIFont systemFontOfSize:15];
            button.layer.borderColor = themeColor.CGColor;
            button.layer.borderWidth = 1;
            [button setTitleColor:themeColor forState:UIControlStateNormal];
            [button setTitle:[week objectAtIndex:i] forState:UIControlStateNormal];
            [button addTarget:self action:@selector(weekAction:) forControlEvents:UIControlEventTouchUpInside];
            [weekDay addSubview:button];
        }
        else
        {
            UIButton * button = [[UIButton alloc]initWithFrame:CGRectMake(10 + 60 * (i - 4), 40, 50, 20)];
            button.layer.cornerRadius = 10;
            button.titleLabel.font = [UIFont systemFontOfSize:15];
            button.layer.borderColor = themeColor.CGColor;
            button.layer.borderWidth = 1;
            [button setTitleColor:themeColor forState:UIControlStateNormal];
            [button setTitle:[week objectAtIndex:i] forState:UIControlStateNormal];
            [button addTarget:self action:@selector(weekAction:) forControlEvents:UIControlEventTouchUpInside];
            [weekDay addSubview:button];
        }
    }
}

-(void)weekAction:(UIButton *)week
{
    week.selected = !week.selected;
    if (week.selected) {
        week.backgroundColor = themeColor;
        [week setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    }
    else
    {
        week.backgroundColor = [UIColor whiteColor];
        [week setTitleColor:themeColor forState:UIControlStateNormal];
    }
    NSString * str = week.titleLabel.text;
    if ([selectedArr containsObject:str])
    {
        [selectedArr removeObject:str];
    }
    else
    {
        [selectedArr addObject:str];
    }
}

-(void)startTimeAction
{
    [self.delegate startTime];
}

-(void)endTimeAction
{
    [self.delegate endTime];
}

-(void)conformAction
{
    NSString * w1 = @"0";
    NSString * w2 = @"0";
    NSString * w3 = @"0";
    NSString * w4 = @"0";
    NSString * w5 = @"0";
    NSString * w6 = @"0";
    NSString * w7 = @"0";

    if ([selectedArr containsObject:@"周一  "])
    {
        w1 = @"1";
    }
    if ([selectedArr containsObject:@"周二  "])
    {
        w2 = @"1";
    }
    if ([selectedArr containsObject:@"周三  "])
    {
        w3 = @"1";
    }
    if ([selectedArr containsObject:@"周四  "])
    {
        w4 = @"1";
    }
    if ([selectedArr containsObject:@"周五  "])
    {
        w5 = @"1";
    }
    if ([selectedArr containsObject:@"周六  "])
    {
        w6 = @"1";
    }
    if ([selectedArr containsObject:@"周日  "])
    {
        w7 = @"1";
    }
    
    if (!beginDay || !endDay)
    {
        [SVProgressHUD showErrorWithStatus:@"请选择时间"];
        return;
    }
    
    [NetRequestCenter enrollwithw1:w1 w2:w2 w3:w3 w4:w4 w5:w5 w6:w6 w7:w7 beginday:beginDay endday:endDay taskid:self.contentId result:^(BOOL success, NSDictionary *resultDic) {
        if (success) {
            [self.delegate finished];
            [self removeAction];
        }
        else
        {
            [SVProgressHUD showErrorWithStatus:[resultDic objectForKey:@"message"]];
        }
    }];
}

-(void)selectStartTime:(NSString *)startTime
{
    beginDay = startTime;
    [timeStart setTitle:startTime forState:UIControlStateNormal];
}

-(void)selectEndTime:(NSString *)endTime
{
    endDay = endTime;
    [timeEnd setTitle:endDay forState:UIControlStateNormal];
}


-(void)removeAction
{
    [self removeFromSuperview];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
